#load data
setwd('/Users/scheung/Desktop/Group_J_Climate_Economics')
finaldata <- read.csv('final_to_use.csv')
#data wrangling
dataco2 <- filter(finaldata, is.na('co2_emission') == FALSE)%>%
  filter(Year > 2009)%>%
  filter(Year < 2016)
country_count <- unique(dataco2['Country'])

#filling in average co2 emissions, arable land
for (i in 1:nrow(country_count)){
  country <- filter(dataco2, Country == country_count[i, 1])
  country_co2 <- country['co2_emission']%>%
    na.omit()
  length_co2 <- nrow(country_co2)
  country_arableland <- country['Arable.Land']%>%
    na.omit()
  length_al <- nrow(country_arableland)
  country_count[i, 'avg_co2'] <-
    mean(country_co2[1:length_co2,'co2_emission'])
  country_count[i, 'arableland'] <- 
    mean(country_arableland[1:length_al,'Arable.Land'])
}

#cutting top 10 countries with highest co2 emissions
top10 <- country_count[order(country_count$avg_co2, decreasing = TRUE,na.last = TRUE),] %>%
  slice(1:10)
plot1 <- ggplot(top10, aes(fill = Country, x= avg_co2, y= arableland, 
                              color = Country)) + 
  geom_point(position="dodge", stat="identity", 
             width = 1, alpha = 0.7, size = 4) +
  xlab('CO2 Emissions') + ylab('Arable Land') + 
  ggtitle("Average Arable Land and CO2 Emissions from 2010-2020")
## Warning: Ignoring unknown parameters: width
plot1
## Warning: Width not defined. Set with `position_dodge(width = ?)`

##Plot #2 Co2 emissions + life expectancy for countries worldwide (on a map)

year2015 <- filter(dataco2, Year == 2015)
#adding life expectancy to the plot data
for (i in 1:nrow(country_count)){
  country_count[i, 'life_expectancy'] <- year2015[i, 'Life.Expectancy']
  country_count[i, 'gdp'] <- year2015[i, 'GDP']
}

#excluding NA values
country_count <- na.omit(country_count)
#finding top 10
country_count <- country_count[order(country_count$avg_co2, 
                                     decreasing = TRUE, na.last = TRUE),]
top10co2 <- country_count[1:10,]
top10co2['rank'] <- 'top 10'

#finding bottom 10
country_count <- country_count[order(country_count$avg_co2, 
                                     decreasing = FALSE, na.last = TRUE),]
bottom10co2 <- country_count[1:10,]
bottom10co2['rank'] <- 'bottom 10'

#binding top and bottom 10 co2 level countries
top10_bottom10 <- rbind(top10co2, bottom10co2)
#Longitude and latitudes for top and bottom 10 countries
longitude <- c(116.20, #China
               -77.02, #US
               77.13, #India
               138.25, #Japan
               13.25, #Germany
               -106.42, #Canada
               -102.55, #Mexico
               113.10, #Indonesia
               -51.92, #Brazil
               22.94, #South Africa
               -175.20, #Tonga
               6.61, #Sao Tome and Principe
               173, #Vanuatu
               -171.00, #Marshall Islands
               43.33, #Comoros
               9.55, #Liechtenstein
               -61.37, #Dominica
               -61.24, #Samoa
               -172.10, #Palau
               -62.7) #St. Kitts and Nevis
latitude <- c(39.55, #China
              39.91, #US
              28.37, #India
              36.20, #Japan
              52.30, #Germany
              56.27, #Canada
              23.63, #Mexico
              -6.09, #Indonesia
              -14.47, #Brazil
              -30.44, #South Africa
              -21.10, #Tonga
              .16, #Sao Tome and Principe
              -17.45, #Vanuatu
              7.13, #Marshall Islands
              -11.64, #Comoros
              47.08, #Liechtenstein
              15.41, #Dominica
              -13.50, #Samoa
              7.20, #Palau
              17.35) #St. Kitts and Nevis

top10_bottom10['longitude'] <- longitude
top10_bottom10['latitude'] <- latitude
popup_content <- paste("Country:",top10_bottom10$Country,"<br/>",
                       "CO2 Emissions:",top10_bottom10$avg_co2,"<br/>",
                       "2015 Life Expetancy:",top10_bottom10$life_expectancy,"<br/>",
                       "2015 GDP:",top10_bottom10$gdp,"<br/>")
pal = colorFactor("Set1", domain = top10_bottom10$rank)
color_avail = pal(top10_bottom10$rank)

#pal = colorFactor("Set1", domain = top20$) <~~ not sure what to set this as
#color_avail = pal(top_expense_rated$top_what) <~~ not sure what to do for this
leaflet(top10_bottom10) %>%
  addTiles() %>%
  addCircles(lng = ~longitude, lat = ~latitude, color = color_avail,
             popup = popup_content)%>%
  addLegend(pal = pal, values = ~top10_bottom10$rank, title = "Type")

Plot #3

Co2 emissions + GDP (how top 10 and bottom 10 GDP countries compare in CO2 emissions from 2010-2015)

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
## 
##     wind
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plot3 <- plot_ly(top10_bottom10, x = ~Country, y = ~life_expectancy, 
                 type = 'bar', color = ~rank)
plot3
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.